package com.superman.supermarket.client.view.search;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.dom.InputEvent;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.supermarket.client.composite.search.SearchTopModule;
import com.superman.supermarket.client.utils.ActivityJumpUrl;

/**
 * 商品搜索
 * 
 * @author superman
 * @version 2018年8月2日下午5:43:36
 */
public class GoodsSearchViewImpl extends Composite implements GoodsSearchView {

	interface GoodsSearchViewImplUiBinder extends UiBinder<Widget, GoodsSearchViewImpl> {
	}

	private static GoodsSearchViewImplUiBinder uiBinder = GWT.create(GoodsSearchViewImplUiBinder.class);

	private GoodsSearchViewPresenter presenter;
	@UiField
	FlowPanel resultpanel;

	@UiField
	SearchTopModule searchbox;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月13日 下午10:35:51
	 */
	public GoodsSearchViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@UiHandler("searchbox")
	void searchByKeyWord(ClickEvent event) {
		if (!StringUtils.isEmpty(searchbox.getValue()))
			presenter.searchByKeyWords(searchbox.getValue());
	}

	@UiHandler("searchbox")
	void searchByKeyWord(InputEvent event) {
		if (!StringUtils.isEmpty(searchbox.getValue()))
			presenter.searchByKeyWords(searchbox.getValue());
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (GoodsSearchViewPresenter) presenter;
	}

	@Override
	public void setSearchReusltList(List<Map<String, Object>> list) {
		resultpanel.clear();
		if (list != null) {

			for (Map<String, Object> map : list) {
				final String spuid = String.valueOf(map.get("SPU_ID"));

				HTML html = new HTML(String.valueOf(map.get("NAME")));
				html.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						ActivityJumpUrl.pushToSkuDetailPage(spuid, "");
					}
				});
				this.resultpanel.add(html);
			}
		} else {
			HTML html = new HTML("抱歉,没有找到你想要的~");
			html.getElement().getStyle().setProperty("color", "#666");
			this.resultpanel.add(html);
		}
	}

}
